package cn.edu.sgu.www.mhxysy.service.impl;

import cn.edu.sgu.www.mhxysy.base.Pager;
import cn.edu.sgu.www.mhxysy.dto.AccountDTO;
import cn.edu.sgu.www.mhxysy.entity.Account;
import cn.edu.sgu.www.mhxysy.entity.RoleAccount;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.AccountMapper;
import cn.edu.sgu.www.mhxysy.mapper.RoleAccountMapper;
import cn.edu.sgu.www.mhxysy.pager.AccountPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.AccountService;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class AccountServiceImpl implements AccountService {

	private final AccountMapper accountMapper;
	private final RoleAccountMapper roleAccountMapper;

	@Autowired
	public AccountServiceImpl(
			AccountMapper accountMapper,
			RoleAccountMapper roleAccountMapper) {
		this.accountMapper = accountMapper;
		this.roleAccountMapper = roleAccountMapper;
	}

	@Override
	public void insert(AccountDTO accountDTO) {
		// 验证账号是否重复
		QueryWrapper<Account> wrapper = new QueryWrapper<>();

		wrapper.eq("account", accountDTO.getAccount());

		long count = accountMapper.selectCount(wrapper);

		if (count > 0) {
			throw new GlobalException(ResponseCode.CONFLICT, "添加失败，账号已存在！");
		} else {
			Account account = new Account();

			account.setId(account.uuid());
			account.setPhone(accountDTO.getPhone());
			account.setAccount(accountDTO.getAccount());

			accountMapper.insert(account);
		}
	}

	@Override
	public void deleteById(String accountId) {
		// 1、验证账号下是否有角色
		QueryWrapper<RoleAccount> wrapper = new QueryWrapper<>();

		wrapper.eq("account_id", accountId);

		// 通过账号ID查询角色数量
		long count = roleAccountMapper.selectCount(wrapper);

		// 角色数量大于0，禁止删除
		if (count > 0) {
			throw new GlobalException(ResponseCode.FORBIDDEN, "删除失败，请先删除该账号下的角色！");
		}

		// 2、删除账号
		accountMapper.deleteById(accountId);
	}

	@Override
	public void updateById(AccountDTO accountDTO) {
		String accountId = accountDTO.getId();

		if (StringUtils.isEmpty(accountId)) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "账号ID不能为空！");
		}

		Account account = accountMapper.selectById(accountId);

		if (account == null) {
			throw new GlobalException(ResponseCode.NOT_FOUND, "修改失败，账号不存在！");
		}

		account.setAccount(accountDTO.getAccount());
		account.setPhone(accountDTO.getPhone());

		accountMapper.updateById(account);
	}

	@Override
	public List<Account> selectAll() {
		return accountMapper.selectList(null);
	}

	@Override
	public Account selectById(String accountId) {
		return accountMapper.selectById(accountId);
	}

	@Override
	public Page<Account> selectByPage(AccountPager pager) {
		QueryWrapper<Account> wrapper = new QueryWrapper<>();
		Page<Account> page = Pager.ofPage(pager);

		wrapper.eq(
				StringUtils.isNotEmpty(pager.getPhone()),
				"phone", pager.getPhone()
		);

		return accountMapper.selectPage(page, wrapper);
	}

}